package com.open.capacity.trade.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Concurrent;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.dto.EditSalaryDto;
import com.open.capacity.trade.model.ConcurrentExcel;
import com.open.capacity.trade.service.ConcurrentService;
import com.open.capacity.trade.vo.ConcurrentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/concurrent")
@Api(tags = "兼职工资API")
public class TradeConcurrentController {
    @Autowired
    private ConcurrentService concurrentService;
    @ApiOperation(value = "查询所有的兼职工资的记录")
    @GetMapping("/findAllConcurrent")
    public PageResult<ConcurrentVo> findAllConcurrent(@RequestParam Map<String,Object> params) throws ControllerException{
        try{
            return concurrentService.findAllConcurrent(params);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @DeleteMapping("/deleteConcurrent/{id}/{concurrentId}")
    @ApiOperation(value = "删除兼职工资记录")
    public Result deleteConcurrent(@PathVariable Long id,@PathVariable Long concurrentId) throws ControllerException{
        try{
            concurrentService.deleteConcurrent(id,concurrentId);
            return Result.succeed("操作成功");
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "修改兼职工资记录")
    public Result saveOrUpdate(@RequestBody Concurrent concurrent) throws ControllerException{
        //是否付款修改,金额
        try{
            return concurrentService.saveOrUpdate(concurrent);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/exportConcurrent")
    @ApiOperation(value = "导出兼职工资管理")
    public void exportConcurrent(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            List<ConcurrentExcel> result = concurrentService.findAllConcurrentExcel(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("兼职工资管理导出", "兼职管理"),
                    ConcurrentExcel.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("/insertConcurrent")
    @ApiOperation(value = "添加兼职工资记录")
    public Result insertConcurrent(@RequestBody Concurrent concurrent) throws ControllerException{
        try{
            return concurrentService.insertConcurrent(concurrent);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }
    @PostMapping("/updateConcurrent")
    @ApiOperation(value = "株连修改兼职工资记录")
    public Result updateConcurrent(@RequestBody Concurrent concurrent) throws ControllerException{
        try{
            return concurrentService.updateConcurrent(concurrent);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }


    @ApiOperation(value = "查找兼职工资记录")
    @PostMapping("/findSalaryByBatchId")
    public Result findSalaryByBatchId(@RequestBody Long id){
        return Result.succeed(concurrentService.findSalaryByBatchId(id),"ok");
    }
    @ApiOperation(value = "查找兼职工资记录")
    @PostMapping("/editSalaryByBatchId")
    public Result editSalaryByBatchId(@RequestBody EditSalaryDto dto){
        boolean flag = concurrentService.editSalaryByBatchId(dto);
        if(flag){
            return Result.succeed("ok");
        }
        return Result.failed("failed");
    }

}

